import 'package:flutter/material.dart';
import 'package:flutter_community/utils/http.dart';
import 'package:flutter_community/utils/showToast.dart';
import 'package:flutter_html/flutter_html.dart';

class DetailPage extends StatefulWidget {
  const DetailPage({super.key});

  @override
  State<DetailPage> createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  Map detailList = {};
  bool isLoading = true;
  //接收参数->由于是二级页面,所以在didChangeDependencies中获取
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    //接收参数
    Map arguments = ModalRoute.of(context)!.settings.arguments as Map;
    // print(arguments);

//判断参数是否为空
    if (arguments.isNotEmpty) {
      getDetailData(arguments['id']);
    } else {
      ToastMessage.showFail('参数错误');
    }
  }

  getDetailData(String id) async {
    try {
      setState(() {
        isLoading = true;
      });
      final res = await http.get('/announcement/$id');
      // print(res);
      if (res['code'] != 10000) {
        ToastMessage.showFail('请求失败');
      } else {
        ToastMessage.showSuccess('数据获取成功');
        //赋值给列表
        setState(() {
          detailList = res['data'];
          isLoading = false;
        });
      }
    } catch (e) {
      ToastMessage.showFail('请求失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('公告详情'),
          backgroundColor: const Color.fromRGBO(255, 247, 255, 1),
        ),
        body: isLoading
            ? const Center(child: CircularProgressIndicator())
            : Container(
                // margin: const EdgeInsets.only(top: 20),
                padding: const EdgeInsets.all(10),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    //头部区域
                    Text(
                      detailList['title'],
                      style: const TextStyle(
                          fontWeight: FontWeight.bold, fontSize: 20),
                    ),
                    const SizedBox(
                      height: 10,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          detailList['creatorName'],
                          style: const TextStyle(color: Colors.grey),
                        ),
                        Text(detailList['createdAt'],
                            style: const TextStyle(color: Colors.grey)),
                      ],
                    ),
                    const SizedBox(height: 20),
                    //底部文字区域
                    Expanded(child: Html(data: detailList['content']))
                  ],
                ),
              ));
  }
}
